<?php

namespace app\common\validate\merchant;
use app\common\model\Merchant\MerchantModel;
use think\Validate;
/**
 * 商户管理验证器
 */
class MerchantValidate extends Validate
{
    // 验证规则
    protected $rule = [
        'ids' => ['require', 'array'],
        'id' => 'require',
        'is_disable' => ['require', 'in' => '0,1'],
        'is_invite' => ['require', 'in' => '0,1'],
        'is_withdrawal' => ['require', 'in' => '0,1'],
        'title'   => ['checkExisted'],
        'merchant_pid'   => ['checkPidExisted'],
        'mer_pwd' => 'require',
        'mer_account' => ['require', 'checkAccountExisted'],
        'group_ids'=> ['require', 'array'],
        'mer_phone'        => ['require', 'mobile'],
    ];
    // 错误信息
    protected $message = [
        'title.require' => '请输入商家名称',
        'mer_pwd.require' => '请输入登录密码',
        'is_invite.require' => '请选择邀请权限',
        'is_withdrawal.require' => '请选择可否提现',
        'mer_account.require' => '请输入登录账号',
        'group_ids.require' => '请选择分组',
        'mer_phone.require'        => '请输入手机号码',
        'mer_phone.mobile'         => '请输入正确的手机号码',
    ];
    // 验证场景
    protected $scene = [
        'info'    =>  ['id'],
        'add'     =>  ['mer_pwd','mer_account','merchant_pid'],
        'edit'    =>  ['id','mer_pwd','mer_account','merchant_pid'],
        'dele'    =>  ['ids'],
        'disable' =>  ['ids', 'is_disable'],
        'invite' =>  ['ids', 'is_invite'],
        'withdrawal' =>  ['ids', 'is_withdrawal'],
        'member_group' =>  ['ids'],
        'phoneRegisterCaptcha' => ['mer_phone'],
        'register'     =>  ['mer_phone','mer_pwd'],
    ];
    // 验证场景定义：手机注册验证码
    protected function scenePhoneRegisterCaptcha()
    {
        return $this->only(['mer_phone'])
            ->append('mer_phone', ['require', 'checkPhoneExisted']);
    }
    // 验证场景定义：注册
    protected function sceneRegister()
    {
        return $this->only(['mer_phone'])
            ->append('mer_phone', ['require', 'checkPhoneExisted']);
    }
    // 自定义验证规则：手机是否已存在
    protected function checkPhoneExisted($value, $rule, $data = [])
    {
        $model = new MerchantModel();
        $pk = $model->getPk();
        $id = $data[$pk] ?? 0;

        $where[] = [$pk, '<>', $id];
        $where[] = ['mer_phone', '=', $data['mer_phone']];
        $where = where_delete($where);
        $info = $model->field($pk)->where($where)->find();
        if ($info) {
            return '手机已存在：' . $data['mer_phone'];
        }

        return true;
    }

    protected function checkPidExisted($value, $rule, $data = [])
    {
        $model = new MerchantModel();
        $pk = $model->getPk();
        $id = $data[$pk] ?? 0;

        $where[] = [$pk, '<>', $id];
        $where[] = ['id', '=', $data['merchant_pid']];
        $where = where_delete($where);
        $info = $model->field($pk)->where($where)->find();
        if (!$info) {
            return '推荐商家不存在：' . $data['merchant_pid'];
        }

        return true;
    }
    protected function checkExisted($value, $rule, $data = [])
    {
        $model = new MerchantModel();
        $pk = $model->getPk();
        $id = $data[$pk] ?? 0;

        $where[] = [$pk, '<>', $id];
        $where[] = ['title', '=', $data['title']];
        $where = where_delete($where);
        $info = $model->field($pk)->where($where)->find();
        if ($info) {
            return '商家已存在：' . $data['title'];
        }

        return true;
    }
    protected function checkAccountExisted($value, $rule, $data = [])
    {
        $model = new MerchantModel();
        $pk = $model->getPk();
        $id = $data[$pk] ?? 0;

        $where[] = [$pk, '<>', $id];
        $where[] = ['mer_account', '=', $data['mer_account']];
        $where = where_delete($where);
        $info = $model->field($pk)->where($where)->find();
        if ($info) {
            return '登录账号已存在：' . $data['mer_account'];
        }

        return true;
    }


}
